<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@page import="com.fick.hibernate.dao.RoleDAO"%>
<%@page import="com.fick.hibernate.model.Role"%>
<%@page import="com.fick.hibernate.model.SysMenu"%>
<%@page import="com.fick.hibernate.dao.SysMenuDAO"%>
<%@page import="com.fick.hibernate.dao.RoleMenuDAO"%>
<%@page import="com.fick.hibernate.model.RoleMenu"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.fick.hibernate.model.Admin"%>
<%@ include file="../tld/tlds.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>角色权限</title>
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.STYLE1 {font-size: 12px;}
.STYLE4 {
	font-size: 12px;
	color: #1F4A65;
	font-weight: bold;
}

a:link {
	font-size: 12px;
	color: #06482a;
	text-decoration: none;

}
a:visited {
	font-size: 12px;
	color: #06482a;
	text-decoration: none;
}
a:hover {
	font-size: 12px;
	color: #FF0000;
	text-decoration: underline;
}
a:active {
	font-size: 12px;
	color: #FF0000;
	text-decoration: none;
}
.STYLE7 {font-size: 12px;}

</style>
<script type="text/javascript" src="js/meihuaxue.js"></script>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" >
function choice(id){
	document.getElementById(id).checked = !document.getElementById(id).checked;
	var objs = document.getElementsByTagName("input");
		var ids=id.split("_");
	if(id.substring(0,1)=="0"){//当子节点有一个选中时，父节点一定要选中
		var parid = ids[0];
		if(document.getElementById(id).checked){
			document.getElementById("1_"+parid).checked = true;
		}
		
		//判断是否全部取消了，若是，则父节点也应自动取消
			var isAll = true;
			for(var i=0;i<objs.length;i++){
				if(objs[i].id!="1_"+parid && objs[i].id.indexOf(parid+"_")!=-1&&objs[i].checked){
					isAll = false;
				}
			}
			if(isAll){
				document.getElementById("1_"+parid).checked = false;
			}
	}
	//alert(objs.length);
	for(var i=0;i<objs.length;i++){
		if(objs[i].id!=null&&objs[i].id!=id&&objs[i].id.indexOf(ids[1]+"_")!=-1){
			objs[i].checked = document.getElementById(id).checked;		
		}
	}
	
}  

</script>
</head>
<%
	RoleDAO dao = new RoleDAO();
	List li = null;
	//得到记录数
	li = null;
	li = dao.findBySql(Role.class,"where id != 3 order by roleOrder");
%>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="30">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="15" height="30"><img src="images/tab_03.gif" width="15" height="30" /></td>
        <td width="1101" background="images/tab_05.gif"><img src="images/311.gif" width="16" height="16" />
			<span class="STYLE4">角色权限管理页面</span></td>
        <td width="281" background="images/tab_05.gif">
        &nbsp;
        </td>
        <td width="14"><img src="images/tab_07.gif" width="14" height="30" /></td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="9" background="images/tab_12.gif">&nbsp;</td>
        <td bgcolor="#f3ffe3">
	  <table width="98%" border="0" cellpadding="0" cellspacing="1" bgcolor="#D7DFE1">
        <tr>
          <td width="60%" height="25" align="left" valign="middle" bgcolor="#FFFFFF" style="padding:10px" class="STYLE1">
		  选择要分配权限的角色:
		    <select name="qx" onchange="sa(this)">
		    <option value="0">请选择
						<% 
							String nowroleid = request.getParameter("roleid");
						if(li!=null){
							Role role;
							for(int i=0;i<li.size();i++)
							{
								role = (Role)li.get(i);
								
								%>
								
<option value="<%=role.getId()%>" <%if(nowroleid!=null&&nowroleid.equals(role.getId()+""))out.print("selected"); %>><%=role.getRoleName() %></option>
								
								<%
							}
						}
						%>
			 </select>
		  </td>
        </tr>
		 
		  <tr>
          <td width="60%" height="25" align="left" valign="middle" bgcolor="#FFFFFF" style="padding:10px" class="STYLE1">
		   选中相应被赋予的权限:
				<div>
				<%
				if(nowroleid!=null && nowroleid!="0" && !nowroleid.equals("0")){
					%>
					<form action="roleMenu.do" method="post">
					<input type="hidden" name="method" value="rm">
					<input type="hidden" name="roleId" value="<%=nowroleid%>">
					<script language="javascript">
					 var tree = new MzTreeView("tree");
					  tree.setIconPath("TreeView/");
					  tree.nodes["0_1"] = "text:系统菜单;";
					  <%

						Admin admin = (Admin) request.getSession().getAttribute("admin");
						List listrm=new RoleMenuDAO().findBySql(RoleMenu.class,"where roleId.id="+nowroleid);
					  	List listhave=new ArrayList();
					  	if(listrm!=null && listrm.size()>0){
					  		for(int i=0;i<listrm.size();i++){
					  			listhave.add(((RoleMenu)listrm.get(i)).getMenuId());
					  		}
					  	}
					  if(admin.getRole().getId() == 3){
						//菜单树结构
						List list=new SysMenuDAO().findAll();
					  	if(list!=null && list.size()>0){
							SysMenu sysMenu;
							for(int i=0;i<list.size();i++){
								String par="1";
								String cur="";
					  			sysMenu=(SysMenu)list.get(i);
					  			if(sysMenu.getParId()!=null){
					  				par="0"+sysMenu.getParId().getId();
					  			}
					  			cur="0"+sysMenu.getId();
					  		%>
					  		tree.nodes["<%=par%>_<%=cur%>"] = "text:<input id=<%=par%>_<%=cur%> type=checkbox name=menus value=<%=sysMenu.getId()%> <%=listhave.contains(sysMenu)?"checked":""%>><%=sysMenu.getMenuName()%>;method:choice('<%=par%>_<%=cur%>')";
							  <%
							}
					  	}
					  	}else{
							List list=new RoleMenuDAO().findBySql(RoleMenu.class,"where roleId.id="+admin.getRole().getId());
						  	if(list!=null && list.size()>0){
								SysMenu sysMenu;
								for(int i=0;i<list.size();i++){
									String par="1";
									String cur="";
						  			sysMenu=((RoleMenu)list.get(i)).getMenuId();
						  			if(sysMenu.getParId()!=null){
						  				par="0"+sysMenu.getParId().getId();
						  			}
						  			cur="0"+sysMenu.getId();
						  		%>
						  		tree.nodes["<%=par%>_<%=cur%>"] = "text:<input id=<%=par%>_<%=cur%> type=checkbox name=menus value=<%=sysMenu.getId()%> <%=listhave.contains(sysMenu)?"checked":""%>><%=sysMenu.getMenuName()%>;method:choice('<%=par%>_<%=cur%>')";
								  <%
								}
						  	}
					  	}
					  %>
					//写入树
			
			
					    document.write(tree.toString());
					    tree.expandAll();
				</script>
					<input type="submit" value="提交" >
				</form>
					<%
				}
				%>
				</div>
       

		   </td>
          
		 </tr>
		  
      </table>
        </td>
        <td width="9" background="images/tab_16.gif">&nbsp;</td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td height="29">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="15" height="29"><img src="images/tab_20.gif" width="15" height="29" /></td>
        <td background="images/tab_21.gif">
        &nbsp;</td>
        <td width="14"><img src="images/tab_22.gif" width="14" height="29" /></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>


</html>
<script language="javascript">
 function sa(selected)
 {
 	var roleid = selected.value;
 	var openurl = "<%=request.getContextPath() %>/admin/role_menu.jsp?roleid="+roleid; 
 	window.location.href=openurl;
 }
 </script>
<script type="text/javascript">
var message = "<%=session.getAttribute("message")%>";
if(message!=null && message!="null" && message !=""){
   alert(message);
  }
</script>
<%
	session.removeAttribute("message");
%>